package kr.or.kosta.otr.persistence.notice;

import java.sql.SQLException;
import java.util.ArrayList;

import javax.annotation.Resource;

import kr.or.kosta.otr.vo.NoticeVO;

import org.springframework.stereotype.Repository;

import com.ibatis.sqlmap.client.SqlMapClient;

@Repository("NoticeDao")
public class NoticeDaoImpl implements NoticeDao {
	@Resource(name="sqlMapClient")
	private SqlMapClient sqlMapClient;
	
	@Override
	public int writeNotice(NoticeVO vo) throws SQLException {
		return (int) sqlMapClient.insert("Notice.writeNotice",vo);
	}

	@Override
	public void updateNotice(NoticeVO vo) throws SQLException {
		sqlMapClient.update("Notice.updateNotice",vo);
	}

	@Override
	public void deleteNotice(int noticeId) throws SQLException {
		sqlMapClient.delete("Notice.deleteNotice",noticeId);
	}

	@Override
	public NoticeVO getOneNotice(int noticeId) throws SQLException {
		return (NoticeVO) sqlMapClient.queryForObject("Notice.getOneNotice",noticeId);
	}

	@Override
	public ArrayList<NoticeVO> getAllNotice() throws SQLException {
		return (ArrayList<NoticeVO>) sqlMapClient.queryForList("Notice.getAllNotice");
	}

	@Override
	public void updateHit(int noticeId) throws SQLException {
		sqlMapClient.update("Notice.updateHits", noticeId);
	}

	@Override
	public ArrayList<NoticeVO> getNoticeListByPage(int pageNo) throws SQLException {
		ArrayList<NoticeVO> list=(ArrayList<NoticeVO>) sqlMapClient.queryForList("Notice.getNoticeListByPage", pageNo);
		for(NoticeVO vo:list){
			vo.getAdminVo().setAdminPassword(null);
			vo.getAdminVo().setAdminPhone(0);
			vo.getAdminVo().setAdminEmail(null);
		}
		return list;
	}

	@Override
	public int getTotalCount() throws SQLException {
		return (int) sqlMapClient.queryForObject("Notice.getTotalCount");
	}
	
	@Override
	public NoticeVO getRecentNotice() throws SQLException {
		return (NoticeVO) sqlMapClient.queryForObject("Notice.getRecentNotice");
	}
}
